package controller.v2;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import model.GoodVO;
import model.MaterialVO;
import model.MemberVO;
import model.SeqVO;
import service.v2.AdService;
import service.v2.ApplyReturnService;
import service.v2.ConfigService;
import service.v2.GoodService;
import service.v2.MaterialService;
import service.v2.MemberService;
import util.PackageUtil;
import util.PropertieUtil;

@Controller
@RequestMapping("/v2/material")
public class MaterialController {
	
	@Autowired
	private MaterialService materialService;
	@Autowired
	private GoodService goodService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private ConfigService configService;
	@Autowired
	private AdService adService;

	@ResponseBody
	@RequestMapping(value="/materialList",method=RequestMethod.GET) 
	public Object materialList(@RequestHeader(value = "SCREENWIDTH", required = false) String screenwidth,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id
			,HttpServletRequest request, HttpServletResponse response, MemberVO memberVO,MaterialVO materialVO) throws Exception{
		int num = materialService.checkGtLevel(member_id,PropertieUtil.loadPropertie("vip_level"));
		if(num <= 0){
			return PackageUtil.packageParams(14001,"会员等级不符","",""); 
		}
		if(materialVO.getState()==null && materialVO.getCreate_by()==null){
			return PackageUtil.packageParams(10001,"缺少必选参数，请参考API文档","",""); 
		}
		//用不上
		/*if(materialVO.getGood_id()==null && materialVO.getGood_id()==null){
			return PackageUtil.packageParams(10001,"缺少必选参数，请参考API文档","",""); 
		}
		int exist_num = goodService.checkGoodById(materialVO.getGood_id());
		if(exist_num==0){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}*/
		if(materialVO.getCreate_by() != null && !member_id.equals(materialVO.getCreate_by())){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		Map map = new HashMap();
		//会员相关
		memberVO = memberService.getAllInfoById(member_id);
		map.put("logo", memberVO.getLogo());
		
		materialVO.setMaterial_cat(PropertieUtil.loadPropertie("material_cat"));
		int total = materialService.getTotal(materialVO);
		materialVO.setPagination(materialVO.getPage_index(), total+"");
		List<MaterialVO> materialList = materialService.getMaterialList(materialVO);
		//缩放图片
		if(screenwidth != null){
			materialService.processImg(materialList, screenwidth,1);
		}
		//分享信息
		materialList = materialService.processShareInfo(materialList);
		//二维码信息  1.分享到商品 2.分享活动页
		materialList = materialService.addSellUrl(materialList ,memberVO);
		map.put("materialList", materialList);
		map.put("pagination", materialVO);
		
		//设置为已读
		adService.setRead(member_id);
		
		return PackageUtil.packageParams(200,"successs",map,""); 
	}

	@ResponseBody
	@RequestMapping(value="/materialInsert",method=RequestMethod.POST) 
	public Object materialInsert(@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id
			,HttpServletRequest request, HttpServletResponse response, MemberVO memberVO,MaterialVO materialVO,String files) throws Exception{
		if(materialVO.getGood_id()==null || "".equals(materialVO.getGood_id())){
			return PackageUtil.packageParams(10001,"缺少必选参数，请参考API文档","",""); 
		}
		if(materialVO.getContent()==null){
			return PackageUtil.packageParams(10001,"缺少必选参数，请参考API文档","",""); 
		}
		if(files==null || files.split(",").length==0){
			return PackageUtil.packageParams(10001,"缺少必选参数，请参考API文档","",""); 
		}
		int num = goodService.checkGoodById(materialVO.getGood_id());
		if(num==0){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		
		materialVO.setCreate_by(member_id);
		materialService.insertMaterial(materialVO,files, PropertieUtil.loadPropertie("material_cat"));
		
		return PackageUtil.packageParams(200,"successs","",""); 
	}
	
	@ResponseBody
	@RequestMapping(value="/materialDelete",method=RequestMethod.POST) 
	public Object materialDelete(@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id
			,HttpServletRequest request, HttpServletResponse response, MemberVO memberVO,MaterialVO materialVO) throws Exception{
		if(materialVO.getMaterial_id()==null){
			return PackageUtil.packageParams(10001,"缺少必选参数，请参考API文档","",""); 
		}
		materialVO.setCreate_by(member_id);
		MaterialVO vo = materialService.checkMaterial(materialVO);
		if(vo==null){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		materialService.backupAndDelete(materialVO.getMaterial_id());
		
		return PackageUtil.packageParams(200,"successs","",""); 
	}
	
	
}
